/*
 * Copyright 2007 Mojo Chen
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

var linkxdEd = {

	onLoad: function() {
		dump('Load editor.js');
		this.strings = document.getElementById("linkxd-strings");
		this.viewTree = document.getElementById('viewTree');
		this.tree = document.getElementById('tree');
		linkxdDb.init();
		// load main links 
		//var links = linkxdDb.getLinks(linkxdDb.linkTypeMain);
		var links = linkxdDb.getTagLinks(linkxdDb.linkTagMain);
		this.fillLinkTree(links);
	},
 
	fillLinkTree: function(links){
		// tree 
		//<treeitem><treerow>
		//<treecell label="joe@somewhere.com"/>
		//<treecell label="Top secret plans"/>
		//</treerow></treeitem>
	
		for (i=0; i<links.length; ++i) {
			var link = links[i];
			var treeitem = document.createElement("treeitem");
			var treerow = document.createElement("treerow");
			// style makeItBlue
			// http://www.xulplanet.com/tutorials/xultu/treestyle.html
			// treerow.setAttribute("properties","makeItBlue");
			treeitem.appendChild(treerow);
			this.newTreeCell(treerow,link.id);
			this.newTreeCell(treerow,link.title);
			this.newTreeCell(treerow,link.url);
			this.newTreeCell(treerow,link.summary);
			//this.newTreeCellCheck(treerow,'true');
			this.viewTree.appendChild(treeitem);
		} // for
	},
 
	clearTree: function(){
		while (this.viewTree.firstChild){
			//The list is LIVE so it will re-index each call
			this.viewTree.removeChild(this.viewTree.firstChild);
		};
	},
  
	newTreeCell:function(treerow,value){
		var aCell = document.createElement("treecell");
		aCell.setAttribute("label",value);
		treerow.appendChild(aCell);
	},
	
   newTreeCellCheck:function(treerow,value){
		var aCell = document.createElement("treecell");
		aCell.setAttribute("value",value);
		treerow.appendChild(aCell);
	},
  
	onMainLinkReloadDb: function(e){
		this.clearTree();
		var links = linkxdDb.getTagLinks(linkxdDb.linkTagMain);
		this.fillLinkTree(links);
	},
	
	onMyLinkReloadDb: function(e){
		this.clearTree();
		var links = linkxdDb.getTagLinks(linkxdDb.linkTagMy);
		this.fillLinkTree(links);
	},
  
	onClearDb: function(e) {
		linkxdDb.clear();
		this.clearTree();
	},
	
	getCurrentRow: function(type){
	
		var selection = this.tree.view.selection;
		// URL,TITLE,SUMMARY
		var result = '';
		
		switch(type){
			case 'ID':
				result = this.tree.view.getCellText(this.tree.currentIndex, this.tree.columns.getColumnAt(0));
				break;
			case 'URL': 
				result = this.tree.view.getCellText(this.tree.currentIndex, this.tree.columns.getColumnAt(2));
				break;
			case 'TITLE':
				result = this.tree.view.getCellText(this.tree.currentIndex, this.tree.columns.getColumnAt(1));
				break;
			case 'SUMMARY':
				result = this.tree.view.getCellText(this.tree.currentIndex, this.tree.columns.getColumnAt(3));
				break;
		}
		return result;
	},
	
	onLinkPaste: function(type){
		var title = this.getCurrentRow('TITLE');
		var url = this.getCurrentRow('URL');
		var summary = this.getCurrentRow('SUMMARY');
		linkxdMain.paste(this.getFormat(type,url,title,summary));
	},
	
	onLinkRemove: function(){
		var id = this.getCurrentRow('ID');
		//alert('remove link ' + id);
		linkxdDb.remove(id);
		this.onReloadDb();
	},
	
	getFormat: function(type,url,title,summary){
		var result = '';
		if(summary != null && summary.length > 0){
			switch(type){
				case 'WIKI':
				  result = linkxdWiki.getQuote(url,title,summary);
				  break;
				case 'BB':
				  result =  linkxdBB.getQuote(url,title,summary);
				  break;
				case 'HTML':
				  result =  linkxdHtml.getQuote(url,title,summary);
				  break;
			}
		} else {
			switch(type){
				case 'WIKI':
				  result = linkxdWiki.getLink(url,title);
				  break;
				case 'BB':
				  result = linkxdBB.getLink(url,title);
				  break;
				case 'HTML':
				  result = linkxdHtml.getLink(url,title);
				  break;
			}
		}
		return result;
	},
	
	onPublish: function(type, tagId) {
		var toPaste = '';
		var links = linkxdDb.getTagLinks(tagId);
		for (i=0; i<links.length; ++i) {
			var link = links[i];
			toPaste += this.getFormat(type,link.url,link.title,link.summary);
			toPaste += '\r\n YOUR_TEXT \r\n';
		}
		linkxdMain.paste(toPaste);
	}
};
window.addEventListener("load", function(e) { linkxdEd.onLoad(e); }, false);
